﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MMO_MANAGER.DTO;
using MMO_MANAGER.BUS;
using MMO_MANAGER.Utils;

namespace MMO_MANAGER.FORM_CATALOG
{
    public partial class frmGpageList : DevExpress.XtraEditors.XtraForm
    {
        public frmGpageList()
        {
            InitializeComponent();
        }

        gpage_list_bus bus;
        gpage_list gpl;
        string chuoi = "mahoa";
        string NotEdit = "Not Edit Data!";
        private void frmGpageList_Load(object sender, EventArgs e)
        {
            bus = new gpage_list_bus();
            gpl = new gpage_list();
            FillData();
        }

        private void FillData()
        {
            gridControl1.DataSource = bus.GetData();
        }

        private void btn_add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Add();
            FillData();
        }

        private void btn_edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit();
            FillData();
        }

        private void btn_delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Delete();
            FillData();
        }

        private void btn_refresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FillData();
        }

        private void btn_help_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        #region Add Edit Delete

        void Add()
        {
            try
            {
                using (var frm = new GPDlg())
                {
                    if (frm.ShowDialog(FindForm()) == DialogResult.OK)
                    {
                        gpl.ten = frm.NameGP;
                        gpl.pwd = EasyEncoding.Encrypt(frm.PWD, chuoi);
                        gpl.trang = frm.Trang;
                        gpl.ghichu = frm.Note;
                        gpl.active = 1;    //  active
                        gpl.ngayedit = DateTime.Now;

                        DataTable dt = bus.GetListGpageByName(gpl);
                        if (dt.Rows.Count == 0)
                        {
                            bus.Insert(gpl);
                            EasyDialog.ShowSuccessfulDialog("Thêm thành công!");
                            //lbl_totalrows.Text = gridView1.RowCount.ToString();
                        }
                        else
                        {
                            EasyDialog.ShowWarningDialog("Đã có email GA này rồi!");
                            return;
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                EasyDialog.ShowErrorDialog("Lỗi: " + ex.Message);
                return;
                throw;
            }
        }

        void Edit()
        {
            try
            {
                int row = gridView1.FocusedRowHandle;
                gpl.id = Convert.ToInt16(gridView1.GetRowCellValue(row, "id"));
                gpl.ten = gridView1.GetRowCellValue(row, "ten").ToString();
                gpl.trang = gridView1.GetRowCellValue(row, "trang").ToString();
                gpl.ghichu = gridView1.GetRowCellValue(row, "ghichu").ToString();

                using (var frm = new GPDlg())
                {
                    frm.txt_pass.ReadOnly = true;
                    frm.NameGP = gpl.ten;
                    frm.PWD = NotEdit;
                    frm.Trang = gpl.trang;
                    frm.Note = gpl.ghichu;

                    if (frm.ShowDialog(FindForm()) == DialogResult.OK)
                    {
                        gpl.ten = frm.NameGP;
                        gpl.trang = frm.Trang;
                        gpl.ghichu = frm.Note;
                        gpl.ngayedit = DateTime.Now;
                        bus.Update(gpl);
                        EasyDialog.ShowSuccessfulDialog("Sửa thành công!");
                    }
                }
            }
            catch (Exception ex)
            {
                EasyDialog.ShowErrorDialog("Lỗi: " + ex.Message);
                return;
                throw;
            }
        }

        void Delete()
        {
            try
            {
                int row = gridView1.FocusedRowHandle;
                gpl.id = Convert.ToInt16(gridView1.GetRowCellValue(row, "id"));
                DialogResult dr = EasyDialog.ShowYesNoDialog("Bạn có muốn xóa không?");
                if (dr == DialogResult.Yes)
                {
                    gpl.active = 0;
                    bus.Delete(gpl);
                    EasyDialog.ShowSuccessfulDialog("Xóa thành công!");
                }
            }
            catch (Exception ex)
            {
                EasyDialog.ShowErrorDialog("Lỗi: " + ex.Message);
                return;
                throw;
            }
        }

        #endregion

        private void btn_copy_email_Click(object sender, EventArgs e)
        {
            int row = gridView1.FocusedRowHandle;
            gpl.ten = gridView1.GetRowCellValue(row, "ten").ToString();
            Clipboard.SetText(gpl.ten);
            EasyDialog.ShowSuccessfulDialog("Copy successfull!");
        }

        private void btn_copy_pwd_Click(object sender, EventArgs e)
        {
            int row = gridView1.FocusedRowHandle;
            gpl.ten = gridView1.GetRowCellValue(row, "pwd").ToString();
            Clipboard.SetText(EasyEncoding.Descrypt(gpl.ten, chuoi));
            EasyDialog.ShowSuccessfulDialog("Copy successfull!");
        }

    }
}